using Microsoft.AspNetCore.Mvc;
using MediPlatform.Read.Api.Models;
using MediPlatform.Read.Api.Services;
using MediPlatform.ErrorCode;

namespace MediPlatform.Read.Api.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class MedicalOrderController : ControllerBase
    {
        private readonly MedicalOrderService _medicalOrderService;
        private readonly ILogger<MedicalOrderController> _logger;

        public MedicalOrderController(
            MedicalOrderService medicalOrderService,
            ILogger<MedicalOrderController> logger)
        {
            _medicalOrderService = medicalOrderService;
            _logger = logger;
        }

        /// <summary>
        /// 获取药品订单分页列表
        /// </summary>
        /// <param name="request">查询请求</param>
        /// <returns>分页结果</returns>
        [HttpGet("paged-list")]
        public async Task<ApiResult<MedicalOrderPagedResponse>> GetPagedList([FromQuery] MedicalOrderQueryRequest request)
        {
            try
            {
                _logger.LogInformation("获取药品订单列表，查询参数：{@Request}", request);

                var result = await _medicalOrderService.GetPagedListAsync(request);

                return ApiResult<MedicalOrderPagedResponse>.Success(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取药品订单列表失败");
                return ApiResult<MedicalOrderPagedResponse>.Error("获取药品订单列表失败");
            }
        }

        /// <summary>
        /// 获取药品订单详情
        /// </summary>
        /// <param name="id">订单ID</param>
        /// <returns>详情信息</returns>
        [HttpGet("detail/{id}")]
        public async Task<ApiResult<MedicalOrderDetailResponse>> GetDetail(long id)
        {
            try
            {
                _logger.LogInformation("获取药品订单详情，ID：{Id}", id);

                var result = await _medicalOrderService.GetDetailAsync(id);

                if (result == null)
                {
                    return ApiResult<MedicalOrderDetailResponse>.Error("药品订单不存在");
                }

                return ApiResult<MedicalOrderDetailResponse>.Success(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取药品订单详情失败，ID：{Id}", id);
                return ApiResult<MedicalOrderDetailResponse>.Error("获取药品订单详情失败");
            }
        }

        /// <summary>
        /// 搜索药品订单
        /// </summary>
        /// <param name="keyword">搜索关键词</param>
        /// <returns>搜索结果</returns>
        [HttpGet("search")]
        public async Task<ApiResult<MedicalOrderPagedResponse>> SearchOrders([FromQuery] string keyword)
        {
            try
            {
                _logger.LogInformation("搜索药品订单，关键词：{Keyword}", keyword);

                var request = new MedicalOrderQueryRequest
                {
                    OrderNoOrUsername = keyword,
                    PageIndex = 1,
                    PageSize = 10
                };

                var result = await _medicalOrderService.GetPagedListAsync(request);
                return ApiResult<MedicalOrderPagedResponse>.Success(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "搜索药品订单失败");
                return ApiResult<MedicalOrderPagedResponse>.Error("搜索药品订单失败");
            }
        }

        /// <summary>
        /// 获取药品订单统计信息
        /// </summary>
        /// <returns>统计信息</returns>
        [HttpGet("statistics")]
        public async Task<ApiResult<MedicalOrderStatisticsResponse>> GetStatistics()
        {
            try
            {
                _logger.LogInformation("获取药品订单统计信息");

                var result = await _medicalOrderService.GetStatisticsAsync();

                return ApiResult<MedicalOrderStatisticsResponse>.Success(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取药品订单统计信息失败");
                return ApiResult<MedicalOrderStatisticsResponse>.Error("获取药品订单统计信息失败");
            }
        }

        /// <summary>
        /// 获取待发货订单列表
        /// </summary>
        /// <returns>待发货订单列表</returns>
        [HttpGet("pending")]
        public async Task<ApiResult<MedicalOrderPagedResponse>> GetPendingOrders()
        {
            try
            {
                var request = new MedicalOrderQueryRequest
                {
                    Status = 0, // 待发货
                    PageIndex = 1,
                    PageSize = 10
                };

                var result = await _medicalOrderService.GetPagedListAsync(request);
                return ApiResult<MedicalOrderPagedResponse>.Success(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取待发货订单失败");
                return ApiResult<MedicalOrderPagedResponse>.Error("获取待发货订单失败");
            }
        }

        /// <summary>
        /// 获取待收货订单列表
        /// </summary>
        /// <returns>待收货订单列表</returns>
        [HttpGet("shipped")]
        public async Task<ApiResult<MedicalOrderPagedResponse>> GetShippedOrders()
        {
            try
            {
                var request = new MedicalOrderQueryRequest
                {
                    Status = 1, // 待收货
                    PageIndex = 1,
                    PageSize = 10
                };

                var result = await _medicalOrderService.GetPagedListAsync(request);
                return ApiResult<MedicalOrderPagedResponse>.Success(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取待收货订单失败");
                return ApiResult<MedicalOrderPagedResponse>.Error("获取待收货订单失败");
            }
        }

        /// <summary>
        /// 获取已收货订单列表
        /// </summary>
        /// <returns>已收货订单列表</returns>
        [HttpGet("delivered")]
        public async Task<ApiResult<MedicalOrderPagedResponse>> GetDeliveredOrders()
        {
            try
            {
                var request = new MedicalOrderQueryRequest
                {
                    Status = 2, // 已收货
                    PageIndex = 1,
                    PageSize = 10
                };

                var result = await _medicalOrderService.GetPagedListAsync(request);
                return ApiResult<MedicalOrderPagedResponse>.Success(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取已收货订单失败");
                return ApiResult<MedicalOrderPagedResponse>.Error("获取已收货订单失败");
            }
        }

        /// <summary>
        /// 获取已取消订单列表
        /// </summary>
        /// <returns>已取消订单列表</returns>
        [HttpGet("cancelled")]
        public async Task<ApiResult<MedicalOrderPagedResponse>> GetCancelledOrders()
        {
            try
            {
                var request = new MedicalOrderQueryRequest
                {
                    Status = 6, // 已取消
                    PageIndex = 1,
                    PageSize = 10
                };

                var result = await _medicalOrderService.GetPagedListAsync(request);
                return ApiResult<MedicalOrderPagedResponse>.Success(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取已取消订单失败");
                return ApiResult<MedicalOrderPagedResponse>.Error("获取已取消订单失败");
            }
        }

        /// <summary>
        /// 清除缓存
        /// </summary>
        /// <returns>操作结果</returns>
        [HttpPost("clear-cache")]
        public ApiResult<object> ClearCache()
        {
            try
            {
                _logger.LogInformation("清除药品订单缓存");

                return ApiResult<object>.Success(new { message = "缓存清除成功" });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "清除药品订单缓存失败");
                return ApiResult<object>.Error("清除缓存失败");
            }
        }
    }
} 